clear*
set maxvar 11000

global date _2025_10_28
cd "/Users/atri0032/Dropbox/World Bank/Temperature, poverty, and inequality/Estimations/"

**********************************************************************************************************************
********************************** Table S13: The effects of temperature on poverty – Subnational GDP analysis
**********************************************************************************************************************

********************************************************************************
**************************** Subnational-level analysis ************************
********************************************************************************

**************************** Panel model ****************************
use "${date}/_data/Kalkuhl and Wenz/panel_short.dta", clear

sort year
	merge m:1 year using "${date}/_data/Kalkuhl and Wenz/Poverty line.dta"
	drop if _merge==2
	drop _merge
	
for var pov_cbn pov_dad: replace X = X*100

_pctile gdp_pc_usd, percentiles(10)	
gen poor_cbn215 = (gdp_pc_usd<= r(r1))

gen poor_cbn215c = .
forval i = 1980/2018 {
	qui sum pov_cbn if year==`i'
	_pctile gdp_pc_usd if year== `i', percentiles(19)	
	replace poor_cbn215c= (gdp_pc_usd<= r(r1)) if year==`i'
}

xtset ID year

gen T_sqr = T_ERA*T_ERA
gen P_sqr = P_ERA*P_ERA

label variable T_sqr "T squared"
label variable P_sqr "P squared"

estimates clear

*** Linear, BHM Model and own Model

replace dT = d.T_ERA
replace dP = d.P_ERA
replace T = T_ERA
replace P = P_ERA


label variable T "Annual mean temperature (°C)"
label variable P "Annual precipitation (1000mm)"
label variable dT "Annual temperature change (°C)"
label variable dP "Annual precipitation change(1000mm)"

label variable T "T"
label variable P "P"
label variable dT "dT"
label variable dP "dP"

encode country,gen(country_id)

for var poor_cbn215: replace X = X*100

keep poor_cbn215 T P year ID
gen sample = "fe_sub"
tempfile fe_sub
save `fe_sub'

reghdfe poor_cbn215 T P , vce(cluster ID) absorb( year ID)
	outreg2 using "${date}/_tables/_tab_s12.xls", replace bdec(3) keep(T P) adjr2
	
**************************** Long difference ****************************
use "${date}/_data/Kalkuhl and Wenz/panel_short.dta", clear

sort year
	merge m:1 year using "${date}/_data/Kalkuhl and Wenz/Poverty line.dta"
	drop if _merge==2
	drop _merge
	
for var pov_cbn pov_dad: replace X = X*100

_pctile gdp_pc_usd, percentiles(12)	
gen poor_cbn215 = (gdp_pc_usd<= r(r1))

xtset ID year

drop if year > 2014
drop if lgdp_pc_usd == .

// ------------------------------
// SET HERE THE TIME INTERVAL (YEARS) FOR THE LONG DIFFERENCE REGRESSION
// ------------------------------
local deltaT = 10
//

gen N_usd=lgdp_pc_usd
gen N_rlcu=lrgdp_pc

sum year
gen y_max = 2014
* gen y_max = r(max)

gen p = floor(year/`deltaT')
sum p
gen p_max = r(max)

gen YY =  year - y_max + `deltaT'*p_max -1
gen period = floor(YY/`deltaT')

collapse(mean) poor_cbn* lgdp_pc_usd temp T_ERA prec P_ERA  ///
	lncumoilgasprod DistRiver_m_MEAN DistCoast_m_MEAN Altitude_MEAN DistPort_m_MEAN ///
  (first) country region_l  wrld1id_0 wrld1iso wrld1name_0 wrld1id_1 wrld1name_1 id_0 iso name_0 id_1 name_1 varname_1 region  ///
  (count) N_usd N_rlcu ///
  , by(ID period) fast

keep if N_usd>= floor(`deltaT'/4)

sum period
gen T_max = r(max)

gen oilgas_mis = lncumoilgasprod==.
replace lncumoilgasprod = 0 if oilgas_mis
 
gen lDistCoast = log(DistCoast_m_MEAN) 
gen lDistRiver = log(DistRiver_m_MEAN)
gen lDistPort = log(DistPort_m_MEAN)
gen Altitude =  Altitude_MEAN/1000 

gen year = period
gen year_sqr = year*year

xtset ID year

gen T_CRU = temp
gen P_CRU = prec
gen dlgdp_pc_usd = d.lgdp_pc_usd

gen dT = d.T_CRU
gen dP = d.P_CRU
gen T = T_CRU
gen P = P_CRU

gen T_sqr = T*T
gen P_sqr = P*P

gen lGDP0 = l.lgdp_pc_usd

label variable T "T"
label variable P "P"
label variable dT "dT"
label variable dP "dP"
label variable T_sqr "T squared"
label variable P_sqr "P squared"
label variable lGDP0 "Log initial GRP"

label variable lDistCoast "Distance coast"
label variable lDistRiver "Distance river"
label variable Altitude "Altitude"
label variable lncumoilgasprod "Cum Oil Gas"
label variable lgdp_pc_usd "Regional GDP (log USD)"

gen dpoor_cbn215 = d.poor_cbn215

encode country,gen(country_id)

keep dpoor_cbn215 dT dP year T_max wrld1id_0
gen sample = "ld_sub"
tempfile ld_sub
save `ld_sub'

reghdfe dpoor_cbn215 dT dP if year==T_max,absorb(i.wrld1id_0)  vce(cluster wrld1id_0)
	outreg2 using "${date}/_tables/_tab_s12.xls", append bdec(3) keep(dT dP) adjr2

********************************************************************************
**************************** Country-level analysis ****************************
********************************************************************************

**************************** Panel model ****************************
use "${date}/_data/Kalkuhl and Wenz/panel_short.dta", clear

sort year
	merge m:1 year using "${date}/_data/Kalkuhl and Wenz/Poverty line.dta"
	drop if _merge==2
	drop _merge
	
for var pov_cbn pov_dad: replace X = X*100

_pctile gdp_pc_usd, percentiles(10)	
gen poor_cbn215 = (gdp_pc_usd<= r(r1))

gen poor_cbn215c = .
forval i = 1980/2018 {
	qui sum pov_cbn if year==`i'
	_pctile gdp_pc_usd if year== `i', percentiles(19)	
	replace poor_cbn215c= (gdp_pc_usd<= r(r1)) if year==`i'
}

xtset ID year

gen T_sqr = T_ERA*T_ERA
gen P_sqr = P_ERA*P_ERA

label variable T_sqr "T squared"
label variable P_sqr "P squared"

estimates clear

*** Linear, BHM Model and own Model

replace dT = d.T_ERA
replace dP = d.P_ERA
replace T = T_ERA
replace P = P_ERA


label variable T "Annual mean temperature (°C)"
label variable P "Annual precipitation (1000mm)"
label variable dT "Annual temperature change (°C)"
label variable dP "Annual precipitation change(1000mm)"

label variable T "T"
label variable P "P"
label variable dT "dT"
label variable dP "dP"

encode country,gen(country_id)

for var poor_cbn215 : replace X = X*100

collapse (mean) poor_cbn215  T P , by(year country_id)

keep poor_cbn215 T P year country_id
gen sample = "fe_na"
tempfile fe_na
save `fe_na'

reghdfe poor_cbn215 T P , vce(cluster country_id) absorb(year country_id)
	outreg2 using "${date}/_tables/_tab_s12.xls", append bdec(3) keep(T P) adjr2
	
**************************** Long difference ****************************
use "${date}/_data/Kalkuhl and Wenz/panel_short.dta", clear

sort year
	merge m:1 year using "${date}/_data/Kalkuhl and Wenz/Poverty line.dta"
	drop if _merge==2
	drop _merge
	
for var pov_cbn pov_dad: replace X = X*100

_pctile gdp_pc_usd, percentiles(12)	
gen poor_cbn215 = (gdp_pc_usd<= r(r1))

xtset ID year

drop if year > 2014
drop if lgdp_pc_usd == .

// ------------------------------
// SET HERE THE TIME INTERVAL (YEARS) FOR THE LONG DIFFERENCE REGRESSION
// ------------------------------
local deltaT = 10
//

gen N_usd=lgdp_pc_usd
gen N_rlcu=lrgdp_pc

sum year
gen y_max = 2014
* gen y_max = r(max)

gen p = floor(year/`deltaT')
sum p
gen p_max = r(max)

gen YY =  year - y_max + `deltaT'*p_max -1
gen period = floor(YY/`deltaT')

collapse(mean) poor_cbn* lgdp_pc_usd temp T_ERA prec P_ERA  ///
	lncumoilgasprod DistRiver_m_MEAN DistCoast_m_MEAN Altitude_MEAN DistPort_m_MEAN ///
  (first) country region_l  wrld1id_0 wrld1iso wrld1name_0 wrld1id_1 wrld1name_1 id_0 iso name_0 id_1 name_1 varname_1 region  ///
  (count) N_usd N_rlcu ///
  , by(ID period) fast

keep if N_usd>= floor(`deltaT'/4)

sum period
gen T_max = r(max)

gen oilgas_mis = lncumoilgasprod==.
replace lncumoilgasprod = 0 if oilgas_mis
 
gen lDistCoast = log(DistCoast_m_MEAN) 
gen lDistRiver = log(DistRiver_m_MEAN)
gen lDistPort = log(DistPort_m_MEAN)
gen Altitude =  Altitude_MEAN/1000 

gen year = period
gen year_sqr = year*year

xtset ID year

gen T_CRU = temp
gen P_CRU = prec
gen dlgdp_pc_usd = d.lgdp_pc_usd

gen dT = d.T_CRU
gen dP = d.P_CRU
gen T = T_CRU
gen P = P_CRU

gen T_sqr = T*T
gen P_sqr = P*P

gen lGDP0 = l.lgdp_pc_usd

label variable T "T"
label variable P "P"
label variable dT "dT"
label variable dP "dP"
label variable T_sqr "T squared"
label variable P_sqr "P squared"
label variable lGDP0 "Log initial GRP"

label variable lDistCoast "Distance coast"
label variable lDistRiver "Distance river"
label variable Altitude "Altitude"
label variable lncumoilgasprod "Cum Oil Gas"
label variable lgdp_pc_usd "Regional GDP (log USD)"

gen dpoor_cbn215 = d.poor_cbn215

encode country,gen(country_id)

collapse (mean) dpoor_cbn215 dT dP, by(wrld1id_0 year T_max)

keep dpoor_cbn215 dT dP year T_max wrld1id_0
gen sample = "ld_na"
tempfile ld_na
save `ld_na'

reghdfe dpoor_cbn215 dT dP,absorb(i.wrld1id_0)  vce(cluster wrld1id_0)
	outreg2 using "${date}/_tables/_tab_s12.xls", append bdec(3) keep(dT dP) adjr2

****************************************************************
**************************** T-test ****************************
****************************************************************

use `fe_sub', clear
	append using `ld_sub'
	append using `fe_na'
	append using `ld_na'
	
**************************** FE vs LD
********** Subnational-level analysis	
estimates clear

qui reg poor_cbn215 T P i.year i.ID if sample=="fe_sub"
	est store fe_sub215

qui reg dpoor_cbn215 dT dP i.wrld1id_0 if year==T_max & sample=="ld_sub"
	est store ld_sub215

qui suest fe_sub215 ld_sub215	
test [fe_sub215_mean]T - [ld_sub215_mean]dT = 0

********** country-level analysis	
qui reg poor_cbn215 T P i.year i.country_id if sample=="fe_na"
	est store fe_na215

qui reg dpoor_cbn215 dT dP i.wrld1id_0 if  sample=="ld_na"
	est store ld_na215

qui suest fe_na215 ld_na215	
test [fe_na215_mean]T - [ld_na215_mean]dT = 0
		
**************************** sub vs national
qui suest fe_sub215 fe_na215	
test [fe_sub215_mean]T - [fe_na215_mean]T = 0

qui suest ld_sub215 ld_na215	
test [ld_sub215_mean]dT - [ld_na215_mean]dT = 0
			